package com.app.piemoment;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.app.piemoment.view.PmDialog;

public abstract class BaseActivity extends Activity implements OnClickListener {
	protected static final int LOAD_PROGRESS = 2012;
	protected int mainViewId;
	protected Button btn_left;
	protected TextView title_text;
	protected Button btn_right;
	public static final String PREF_NAME = "PREF_NAME";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(mainViewId);

		btn_left = (Button) findViewById(R.id.btn_left);
		title_text = (TextView) findViewById(R.id.text_title);
		btn_right = (Button) findViewById(R.id.btn_right);
		
		
		initMainView();
		registerMainViewEvent();
		processMainView();

	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOAD_PROGRESS:
			PmDialog dialog = new PmDialog(this);
			return dialog;

		default:
			break;
		}
		return super.onCreateDialog(id);
	}

	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		super.onPrepareDialog(id, dialog);
	}

	protected void initTitle(int leftDrawable, int titleId, int rightId,
			int rightText) {
		
		if (btn_left != null) {
			if (0 != leftDrawable) {
				btn_left.setBackgroundResource(leftDrawable);
			}
			btn_left.setOnClickListener(this);
		}
		if (title_text != null) {
			if (0 != titleId) {
				title_text.setText(getResources().getString(titleId));
			}
		}

		if (btn_right != null) {
			if (0 != rightId) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setBackgroundResource(rightId);
			} else {
				btn_right.setVisibility(View.GONE);
			}
			if (rightText != 0) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setText(getResources().getString(rightText));
			}
			btn_right.setOnClickListener(this);
		}
	}

	protected void initTitle(int leftDrawable, String titleText, int rightId,
			int rightText) {
		
		if (btn_left != null) {
			if (0 != leftDrawable) {
				btn_left.setBackgroundResource(leftDrawable);
			}
			btn_left.setOnClickListener(this);
		}
		if (title_text != null) {
			title_text.setText(titleText);
		}

		if (btn_right != null) {
			if (0 != rightId) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setBackgroundResource(rightId);
			} else {
				btn_right.setVisibility(View.GONE);
			}
			if (rightText != 0) {
				btn_right.setVisibility(View.VISIBLE);
				btn_right.setText(getResources().getString(rightText));
			}
			btn_right.setOnClickListener(this);
		}
	}
	/**
	 * 更新title
	 * @param nickname
	 */
	public void updateTitle(String nickname) {
		if (title_text != null) {
			title_text.setText(nickname);
		}
	}
	
	
	protected abstract void initMainView();

	protected abstract void registerMainViewEvent();

	protected abstract void processMainView();

	
}
